<?php
/**
 * Created by PhpStorm.
 * User: Victor
 * Date: 2018/10/10
 * Time: 9:25
 */

namespace app\manage\controller;
use think\Controller;
use think\Db;
use think\Request;

class Xiushi extends BaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods:POST,GET");
        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        header("Content-type:text/json;charset=utf-8");
    }
    public function index()
    {
        $data = input();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $shuju = Db::query('SELECT *,(SELECT name FROM heyue WHERE xiushi.heyue_id = heyue.id) as heyuename FROM xiushi');
            foreach ($shuju as $key => $value){
                if($value['shijian']==0 || $value['shijian']=='' ){
                    $shuju[$key]['shijian'] = '';
                }else{
                    $shuju[$key]['shijian'] = date('Y-m-d H:i:s', $value['shijian']);
                }
                if($value['shijian1']==0 || $value['shijian1']=='' ){
                    $shuju[$key]['shijian1'] = '';
                }else{
                    $shuju[$key]['shijian1'] = date('Y-m-d H:i:s', $value['shijian1']);
                }
            }
            $result['code']=1;
            $result['shuju']= $shuju;
            $result['msg']=urlencode('请求数据成功');
            return urldecode(json_encode($result));
        }else{
            $result['code']=0;
            $result['msg']=urlencode('该用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function xiushi_add_shuju()
    {
        $heyue = db('heyue')
            ->field('id,name')
            ->select();
        $result['code']=1;
        $result['shuju']= $heyue;
        $result['msg']=urlencode('666');
        return urldecode(json_encode($result));
    }
    public function xiushi_add()
    {
        $data = input();
        if(empty($data['token']) || empty($data['heyue_id']) || empty($data['shijian']) || empty($data['shijian1']) || empty($data['beizhu'])){
            $result['code']=0;
            $result['msg']=urlencode('请求格式有误！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            unset($data['token']);
            $data['shijian'] = strtotime($data['shijian']);
            $data['shijian1'] = strtotime($data['shijian1']);
            $res = db('xiushi')
                ->insertGetId($data);
            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('请求数据成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('新增失败！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('该用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function xiushi_delete()
    {
        $data = input();
        if(empty($data['token']) || empty($data['id'])){
            $result['code']=0;
            $result['msg']=urlencode('请求格式有误！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $res = db('xiushi')
                ->where('id',$data['id'])
                ->delete();
            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('删除数据成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('删除失败！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('该用户不存在！');
            return urldecode(json_encode($result));
        }
    }
}